package cn.picclife.mwx.manager.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.TTaskMapper;
import cn.picclife.mwx.manager.sys.mapper.TTaskNodeApproveMapper;
import cn.picclife.mwx.manager.sys.mapper.TTaskNodeMapper;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.po.TTask;
import cn.picclife.mwx.manager.sys.model.po.TTaskNode;
import cn.picclife.mwx.manager.sys.model.po.TTaskNodeApprove;
import cn.picclife.mwx.manager.sys.model.po.TTaskNodeExample;
import cn.picclife.mwx.manager.sys.model.po.TTaskNodeExample.Criteria;
import cn.picclife.mwx.manager.sys.service.TaskMgMtService;
import cn.picclife.mwx.manager.util.Constant;


@Service
public class TaskMgMtServiceImpl implements TaskMgMtService {

	@Autowired
	private TTaskMapper tTaskMapper;
	
	@Autowired
	private TTaskNodeApproveMapper tTaskNodeApproveMapper;
	
	@Autowired
	private TTaskNodeMapper tTaskNodeMapper; 
	
	@Override
	public PageCount queryTask(PageCount pageCount) {
		int allCount = tTaskMapper.queryTaskAllCount(pageCount.getObject());
		int allPages = allCount/pageCount.getRowNum();
		if (allCount%pageCount.getRowNum() > 0) {
			allPages += 1; 
		}
		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		List<TTask> taskList = tTaskMapper.queryTask(pageCount);
		pageCount.setDatas(taskList);
		return pageCount;
	}


	@Override
	public TTask queryByTaskId(Integer taskId) {
		return tTaskMapper.selectByPrimaryKey(taskId);
	}


	@Override
	public List<Map<String,String>> getTaskNodeApprove(Integer taskId) {
		return tTaskNodeApproveMapper.queryTaskNodeApprove(taskId);
	}


	@Override
	public boolean saveTaskNodeApprove(int taskId, int nodeId, String approveMemo, String optNo,int approveState) {
		//新增节点审核
		TTaskNodeApprove record = new TTaskNodeApprove();
		record.setApproverId(optNo);
		record.setApproveMemo(approveMemo);
		record.setNodeId(nodeId);
		record.setTaskId(taskId);
		record.setApproveTime(new Date());
		record.setApproveState(approveState);
		int addRows = tTaskNodeApproveMapper.insert(record);
		
		//同步更新节点的审核状态
		TTaskNode taskNode = new TTaskNode();
		taskNode.setNodeId(nodeId);
		taskNode.setApproveState(approveState);
		taskNode.setTaskId(taskId);
		int updateRows = tTaskNodeMapper.updateByPrimaryKeySelective(taskNode);
		
		return addRows*updateRows == 1 ? true:false;
	}


	@Override
	public boolean updateTaskNodeApprove(int taskId, int nodeId, String approveMemo, String optNo, int approveState) {
		//修改节点审核  更新t_task_node_approve
		TTaskNodeApprove record = new TTaskNodeApprove();
		record.setApproverId(optNo);
		record.setApproveMemo(approveMemo);
		record.setNodeId(nodeId);
		record.setTaskId(taskId);
		record.setApproveTime(new Date());
		record.setApproveState(approveState);
		int addRows = tTaskNodeApproveMapper.updateByTaskIdNodeId(record);
		int updateRows=1;
		if(approveState==3){
			//同步更新节点的审核状态 t_task_node
			TTaskNode taskNode = new TTaskNode();
			taskNode.setNodeId(nodeId);
			taskNode.setApproveState(approveState);
			taskNode.setTaskId(taskId);
			updateRows= tTaskNodeMapper.updateByPrimaryKeySelective(taskNode);
		}else{
			//同步更新节点的审核状态 t_task_node
			TTaskNode taskNode = new TTaskNode();
			taskNode.setNodeId(nodeId);
			taskNode.setApproveState(approveState);
			taskNode.setTaskId(taskId);
			updateRows= tTaskNodeMapper.updateNodeApproveState(taskNode);
		}
		
		return addRows*updateRows == 1 ? true:false;
	}

	@Override
	public int queryTaskNodeCount(Integer taskId) {
		TTaskNodeExample example = new TTaskNodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTaskIdEqualTo(taskId);
		return tTaskNodeMapper.countByExample(example);
	}

	
	@Override
	public int queryTaskNodePassCount(Integer taskId) {
		return tTaskNodeMapper.queryTaskNodePassCount(taskId);
	}

	@Override
	public int queryTaskNodeNoPassCount(Integer taskId) {
		TTaskNodeExample example = new TTaskNodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTaskIdEqualTo(taskId);
		criteria.andApproveStateEqualTo(Constant.TASK_NODE_NO_PASS);
		return tTaskNodeMapper.countByExample(example);
	}

	@Override
	public void updateTask(int taskId, int taskState,Date approveTime) {
		TTask task  = new TTask();
		task.setTaskId(taskId);
		task.setTaskState(taskState);
		task.setApproveTime(approveTime);
		tTaskMapper.updateTask(task);
	}


	@Override
	public int queryTaskNodeApproveCount(Integer taskId) {
		TTaskNodeExample example = new TTaskNodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTaskIdEqualTo(taskId);
		criteria.andApproveStateGreaterThan(Constant.TASK_NODE_NO_APPROVE);
		return tTaskNodeMapper.countByExample(example);
	}


	@Override
	public String QueryFileUrl(String fileId) {
		return tTaskNodeMapper.QueryFileUrl(fileId);
	}


	@Override
	public void updateTaskNoPass(int taskId, int taskStateNoPass, Date date) {
		tTaskMapper.updateTaskNoPass(taskId);
		
	}


	@Override
	public int queryNodeApproveCounts(int taskId) {
		return tTaskMapper.queryNodeApproveCounts(taskId);
	}


	@Override
	public TTask queryNodeByfileid(String fileId) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public TTaskNode queryByNodeId(int taskId, int nodeId) {
		TTaskNode taskNode = new TTaskNode();
		taskNode.setNodeId(nodeId);
		taskNode.setTaskId(taskId);
		return tTaskNodeMapper.queryByNodeId(taskNode);
	}





}
